<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/all.conf.php');
require_once(__PHY_WEB.__DIR_ADM.'/admin.inc.php');
require_once(__PHY_LIB.'/class/uCategory.class.php');

$rReq = validate($_REQUEST, 'msg:u|action:e[list,edit,new,add,update,delete]');
$sMsg = $rReq['msg'];
$sAction = $rReq['action'];

if ('add' == $sAction) {
	do {
		$rP = validate($_POST, 'parent_id:n|name:u|title:U|title_en:u');
		if (!$rP) { $sMsg = '标题为空'; break; }
		$oCtgry = new uCategory();
		$oCtgry->set('parent_id', $rP['parent_id']);
		$oCtgry->set('name', $rP['name']);
		$oCtgry->set('title', $rP['title']);
		$oCtgry->set('title_en', $rP['title_en']);
		$oCtgry->set('rank', $oCtgry->calcNewRank());
		if ($x = $oCtgry->invalid()) {
			$sMsg = '出错啦。原因：'.$x; break;
		}
		elseif ($oCtgry->insert()) {
			$oCtgry->set('depth', $oCtgry->calcDepth());
			$oCtgry->set('tree', $oCtgry->calcTree());
			$oCtgry->update();
			$oCtgry->refreshChildrenByParents($oCtgry->getID());
			$sMsg = '添加成功'; break;
		}
		else {
			$sMsg = '添加失败！！'; break;
		}
	} while (false);
	$sAction = 'list';
}
elseif ('update' == $sAction) {
	do {
		$rP = validate($_POST, 'id:N|rank:N|name:u|title:U|title_en:u');
		if (!$rP) { $sMsg = '标题为空，或者序号错误'; break; }
		$oCtgry = new uCategory($rP['id']);
		$oCtgry->set('name', $rP['name']);
		$oCtgry->set('title', $rP['title']);
		$oCtgry->set('title_en', $rP['title_en']);
		if (!$oCtgry->update()) { $sMsg = '更新失败！！'; break; }
		if (!$oCtgry->rankTo($rP['rank'])) { $sMsg = '更新序号失败！！'; break; }
		$sMsg = '修改成功'; break;
	} while (false);
	$sAction = 'list';
}
elseif ('delete' == $sAction) {
	do {
		$rP = validate($_POST, 'id:N');
		if (!$rP) { $sMsg = '标题为空，或者序号错误'; break; }
		$oCtgry = new uCategory($rP['id']);
		if (!(0 < $oCtgry->getID())) { $sMsg = '该类别不存在。'; break; }
		if (0 < $oCtgry->countChildrenByParents($oCtgry->getID())) { $sMsg = '无法删除，里面还有子类。'; break; }
		if ($oCtgry->delete()) { $sMsg = '删除成功'; break; }
		else { $sMsg = '删除失败！！'; break; }
	} while (false);
	$sAction = 'list';
}

if ('new' == $sAction) {
	$rP = validate($_GET, 'parent:n');
	$oTpl->assign('rFilter', $rP);
	$oCtgry = new uCategory($rP['parent']);
	if ($r = $oCtgry->fetchBreadCrumb()) {
		$oTpl->assign('rBreadCrumb', $r);
	}
}
elseif ('edit' == $sAction) {
	$oOne = new uCategory($_REQUEST['id']);
	$oTpl->assign('rOne', $oOne->getData());
	$oCtgry = new uCategory($oOne->get('parent_id'));
	if ($r = $oCtgry->fetchBreadCrumb()) {
		$oTpl->assign('rBreadCrumb', $r);
	}
	$oList = new uCategoryList();
	$oList->cfgWhere("parent_id='".$oOne->get('parent_id')."'");
	$oList->cfgOrder("rank");
	$oList->load();
	$oTpl->assign('rSlctRanks', $oList->fetchArray());
}
elseif ('list' == $sAction) {
	$oList = new uCategoryList();
	$oList->cfgOrder("tree");
	$oList->load();
	while ($oOne = $oList->nextItem()) {
		$oOne->set('BreadCrumb', $oOne->fetchBreadCrumb());
	}
	$oTpl->assign('rList', $oList->fetchArray());
}

$oTpl->assign('sMsg', $sMsg);
$oTpl->assign('sAction', $sAction);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
?>
